package com.gorkr.pan.swagger2;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 接口文档配置类
 */
@SpringBootConfiguration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@Log4j2
public class Swagger2Config {
    @Autowired
    private Swagger2ConfigProperties properties;

    @Bean
    public Docket panServerApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .enable(properties.isShow())// 是否启用文档
                .groupName(properties.getGroupName())// 分组名称
                .apiInfo(apiInfo()) // API 信息
                .useDefaultResponseMessages(false)// 不使用默认响应消息
                .select()
                .apis(RequestHandlerSelectors.basePackage(properties.getBasePackage()))// 扫描的包路径
                .paths(PathSelectors.any())// 所有路径都生成文档
                .build();
        log.info("The swagger2 have been loaded successfully!");
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(properties.getTitle())   // 文档标题
                .description(properties.getDescription())  // 文档描
                .termsOfServiceUrl(properties.getTermsOfServiceUrl())  // 服务条款URL
                .contact(new Contact(properties.getContactName(), properties.getContactUrl(), properties.getContactName()))  // 联系人信息
                .version(properties.getVersion())// API版本
                .build();
    }
}
